<?php
/**
 * Created by PhpStorm.
 * User: eeo
 * Date: 2017/7/11
 * Time: 11:41
 */

namespace App\Http\Controllers\Foundation;

use App\Model\Business\Appointment;
use App\Model\Business\AppointmentSchedule;
use App\Model\Business\Classe;
use App\Model\Business\Schedule;
use App\Model\Eeoapi\Eeocomment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Model\System\Token;
use App\Http\Controllers\MessagePacket;


class FeedbackController extends BaseController
{
    use MessagePacket;

    /**
     * 获取某个学生的课堂反馈
     * @param $id
     * @return packetMessage
     */
    function get(Request $request,$id)
    {
        $id = intval($id);
        $query = DB::table('eeo_comments')
            ->select('eeo_comments.*','schedules.name as schedule','schedules.begintime')
            ->where('eeo_comments.studentid',$id)
            ->join('schedules', 'schedules.scheduleid', '=', 'eeo_comments.scheduleid')
            ->orderBy('schedules.begintime','desc');

        # 需要／不需要 分页
        if($request->has('page')) {
            $limit = 30;
            if($request->has('perPage')){
                $limit = $request->get('perPage');
            }
            $info = $query->paginate($limit);
            $comments = $info->items();
        }else{
            $comments = $query->get();
        }

        foreach($comments as $key => $comment){
            $schedule = Schedule::where('scheduleid',$comment->scheduleid)->first();
            ##班课,约课
            if($schedule->type == Schedule::LESSON){
                $comment->course = Classe::where('classuid',$schedule->classeid)
                    ->value('coursename');
            }
            else{
                $appointid = AppointmentSchedule::where('scheduleid',$schedule->scheduleid)->value('id');
                $comment->course = Appointment::where('appointid',$appointid)->value('coursename');
            }
            $comments[$key] = $comment;
        }

        # 需要／不需要 分页
        if($request->has('page')) {
            $info = $info->toArray();
            $data['list'] = $comments;
            unset($info['data']);
            $data['pager'] = $info;
        }else{
            $data = $comments;
        }

        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }

    /**
     * 获取所有学生的课堂反馈
     * @param $id
     * @return packetMessage
     */
    function gets(Request $request)
    {
        $query = DB::table('eeo_comments')
            ->select('eeo_comments.*','schedules.name as schedule','schedules.begintime','students.ClassInNo'
            ,'students.name','students.mobile')
            ->join('schedules', 'schedules.scheduleid', '=', 'eeo_comments.scheduleid')
            ->join('students', 'students.studentuid', '=', 'eeo_comments.studentid')
            ->orderBy('schedules.begintime','desc');

        if($request->has('name')) {
            $name = $request->get('name');
            $query->orwhere('students.name','like',"%$name%");
        }

        if($request->has('schedule')) {
            $schedule = $request->get('schedule');
            $query->orwhere('schedules.name','like',"%$schedule%");
        }

        if($request->has('mobile')) {
            $mobile = $request->get('mobile');
            $query->orwhere('students.mobile','like',"%$mobile%");
        }

        # 需要／不需要 分页
        if($request->has('page')) {
            $limit = 30;
            if($request->has('perPage')){
                $limit = $request->get('perPage');
            }
            $info = $query->paginate($limit);
            $comments = $info->items();
        }else{
            $comments = $query->get();
        }

        foreach($comments as $key => $comment){
            $schedule = Schedule::where('scheduleid',$comment->scheduleid)->first();
            ##班课,约课
            if($schedule->type == Schedule::LESSON){
                $comment->course = Classe::where('classuid',$schedule->classeid)
                    ->value('coursename');
            }
            else{
                $appointid = AppointmentSchedule::where('scheduleid',$schedule->scheduleid)->value('id');
                $comment->course = Appointment::where('appointid',$appointid)->value('coursename');
            }
            $comments[$key] = $comment;
        }

        # 需要／不需要 分页
        if($request->has('page')) {
            $info = $info->toArray();
            $data['list'] = $comments;
            unset($info['data']);
            $data['pager'] = $info;
        }else{
            $data = $comments;
        }

        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }
}

